package com.example.redislock.config;

import com.example.redislock.core.RedisReentrantLock;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * 分布式锁自动配置类
 */
@Configuration
@ConditionalOnClass(RedisReentrantLock.class)
@EnableConfigurationProperties(RedisLockProperties.class)
public class RedisLockAutoConfiguration {


    /**
     * 锁工厂Bean，用于创建具体的锁实例
     */
    @Bean
    @ConditionalOnMissingBean
    public RedisLockFactory redisLockFactory(RedisTemplate<String, Object> redisTemplate,
                                           RedisLockProperties properties) {
        return new RedisLockFactory(redisTemplate, properties);
    }
}
